//
//  HMVisitorTableViewController.swift
//  MicroBlog
//
//  Created by Apple on 16/9/20.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit

class HMVisitorTableViewController: UITableViewController, HMVisitorViewDelegate {

    //是否登录标识
    var isLogin: Bool = HMUserAccountViewModel.sharedUserAccountViewModel.isLogin
    
    var visitorView: HMVisitorView?
    //自定义view视图,重写loadView
    override func loadView() {
        if isLogin {
            super.loadView()
        } else {
            //自定义视图
            visitorView = HMVisitorView()
            visitorView?.loginClosoure = { [weak self] in
                print("我是访客视图闭包调用过来的")
                self?.requestLoginOperation()
            }
            //指定代理
            visitorView?.delegate = self
            view = visitorView
            //未登录时添加导航栏按钮
            setupUI()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

    }
    
    //设置导航栏按钮
    private func setupUI() {
        navigationItem.rightBarButtonItem = UIBarButtonItem.init(title: "注册", imageName: nil, target: self, action: #selector(loginAction))
        navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: "登录", imageName: nil, target: self, action: #selector(loginAction))

    }
    
    //点击事件
    @objc private func loginAction() {
        print("我是访客顶部视图点击的操作")
        requestLoginOperation()
    }
    
    //代理方法
    func didSelectedLogin() {
        print("我是访客视图代理调用过来的")
    }
    //请求登录的操作
    private func requestLoginOperation() {
        let OAuthVC = HMOAuthViewController()
        let nav = HMNaviViewController(rootViewController: OAuthVC)
        present(nav, animated: true, completion: nil)
    }
}
